<?php 

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
define("DEBUG",false);
class Table extends CI_Controller {

	public function index()
	{
		
	}
	function dates()
	{
		$id=(int)$_GET['id'];
		if($id==0)
		{
			die('hack');
		}
		$callback = $_GET["callback"];
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/json");
		$this->load->model('json');
		$cont=$this->json->dates($id);
		echo $callback.'('.$cont.')';
	}
	function get()
	{
		$id=(int)$_GET['id'];
		$y=(int)$_GET['year'];
		$m=(int)$_GET['month'];
		
		$callback = $_GET["callback"];
		if($y==0||$m==0||$id==0)
		{
			die('hack');
		}
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/json");
		$this->load->model('json');
		$agent=17;
		if(!file_exists("cache/p".$id."_".$agent."_".$y.$m.".json") or DEBUG)
		{
			$this->load->model('json');
			$cont=$this->json->get($y,$m,$agent,$id);
			//$cont = $this->json->getlist($client,$agent,$type);
			$fh = fopen("cache/p".$id."_".$agent."_".$y.$m.".json", "w");
			fwrite($fh,$cont);
			fclose($fh);
			echo $callback.'('.$cont.')';
		}
		else
		{
			$filename = "cache/p".$id."_".$agent."_".$y.$m.".json";
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize($filename));
			echo $callback.'('.$contents.')';
		}
		
	
	}
	function get2()
	{
		$id=(int)$_GET['id'];
		$d1=(isset($_GET['dateS'])) ? mysql_escape_string(htmlspecialchars($_GET['dateS'])) : 0;
		$d2=(isset($_GET['dateE'])) ? mysql_escape_string(htmlspecialchars($_GET['dateE'])) : 0;
	
		$callback = $_GET["callback"];
		if($d1==0||$d2==0||$id==0)
		{
			die('hack');
		}
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/json");
		$this->load->model('json');
		//$agent=17;
		//if(!file_exists("cache/p".$id."_".$agent."_".$y.$m.".json") or DEBUG)
		//{
			$this->load->model('json');
			$cont=$this->json->get2($d1,$d2,$id);
			//$cont = $this->json->getlist($client,$agent,$type);
			//$fh = fopen("cache/p".$id."_".$agent."_".$y.$m.".json", "w");
			//fwrite($fh,$cont);
			//fclose($fh);
			echo $callback.'('.$cont.')';
		/*}
		else
		{
			$filename = "cache/p".$id."_".$agent."_".$y.$m.".json";
			$handle = fopen($filename, "r");
			$contents = fread($handle, filesize($filename));
			echo $callback.'('.$contents.')';
		}*/
	
	
	}
	public function price()
	{
		$date1=(isset($_GET['date1'])) ? mysql_escape_string(htmlspecialchars($_GET['date1'])) : 0;
		$date2=(isset($_GET['date2'])) ? mysql_escape_string(htmlspecialchars($_GET['date2'])) : 0;
		$id=(isset($_GET['id'])) ? (int)($_GET['id']) : 0;
		$type=(isset($_GET['type'])) ? (int)($_GET['type']) : 0;
		$callback = (isset($_GET["callback"])) ? $_GET["callback"] : 0;
		
	
		
		/*echo $callback."<<<<<<<<<<<->>>>>>>>>>>".$oto."<<<<<<<<->>>>>>>>".$date;
		if($callback==0||$oto==0||$date==0)
		{
			die('hack');
		}*/
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/json");
		$this->load->model('json');
		
		$cont=$this->json->price($date1,$date2,$id,$type);
		echo $callback.'('.$cont.')';
	}
	public function t_price()
	{
		$date=(isset($_GET['date'])) ? mysql_escape_string(htmlspecialchars($_GET['date'])) : 0;
		$cat=(isset($_GET['category'])) ? mysql_escape_string(htmlspecialchars($_GET['category'])) : 0;
		$oto=(isset($_GET['id'])) ? (int)($_GET['id']) : 0;
		$type=(isset($_GET['type'])) ? (int)($_GET['type']) : 0;
		$callback = (isset($_GET["callback"])) ? $_GET["callback"] : 0;
	
	
	
		/*echo $callback."<<<<<<<<<<<->>>>>>>>>>>".$oto."<<<<<<<<->>>>>>>>".$date;
			if($callback==0||$oto==0||$date==0)
		{
		die('hack');
		}*/
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/json");
		$this->load->model('json');
	
		$cont=$this->json->t_price($date,$oto,$type,$cat);
		echo $callback.'('.$cont.')';
	}
	function search_date()
	{
		$id=(isset($_GET['id'])) ? (int)($_GET['id']) : 0;
		
		$date=(isset($_GET['date'])) ? mysql_escape_string(htmlspecialchars($_GET['date'])) : 0;
		$callback = (isset($_GET["callback"])) ? $_GET["callback"] : 0;
		header('Access-Control-Allow-Origin: *');
		header ("Content-Type:text/json");
		$this->load->model('json');
		
		$cont=$this->json->search_date($date,$id);
		echo $callback.'('.$cont.')';
		
	}
}
?>